package net.jsoft.pms.service.mysql.problem.impl;

import java.io.InputStream;
import java.sql.Clob;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.proxy.jdbc.ClobProxy;

import net.jsoft.platform.security.entity.user.SysUser;
import net.jsoft.platform.security.repository.UserRepository;
import net.jsoft.pms.entity.dept.Dept;
import net.jsoft.pms.entity.problem.ProblemTask;
import net.jsoft.pms.repository.problem.ProblemRepository;
import net.jsoft.pms.service.mysql.problem.IProblemService;
import net.jsoft.pms.util.ReadExcelUtils;

@Service
public class ProblemServiceImpl implements IProblemService
{
    //    private static final Logger logger = LogManager.getLogger();
    private ReadExcelUtils excelReader = null;

    @Autowired
    private ProblemRepository repository;

    @Autowired
    private UserRepository userRepository;

    private Map<String, SysUser> userMap = new HashMap<String, SysUser>();

    @Override
    public void batchImport(InputStream inputStream)
    {
        excelReader = new ReadExcelUtils(inputStream, ReadExcelUtils.EXCEL2007);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<Integer, Map<Integer, Object>> map = null;
        List<ProblemTask> problemTaskBeans = null;
        try
        {
            map = excelReader.readExcelContent(0);
            problemTaskBeans = new ArrayList<ProblemTask>();
            ProblemTask p = null;
            Dept dept1 = userRepository.findDept(1);
            Dept dept2 = userRepository.findDept(2);
            Dept dept3 = userRepository.findDept(3);
            for (int i = 1; i <= map.size(); i++)
            {
                p = new ProblemTask();
                Map<Integer, Object> m = map.get(i);
                p.setId(m.get(0) + "");
                p.setLevel(m.get(5) + "");
                p.setTitle(((String) m.get(9)).replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", ""));
                p.setContent(((String) m.get(10)).replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", ""));

                String k = (String) m.get(14);
                if (userMap.containsKey(k))
                {
                    p.setOptPerson(userMap.get(k));

                }
                else
                {
                    SysUser sysUser = userRepository.findUserByFullName(k);
                    if (sysUser != null)
                    {
                        userMap.put(k, sysUser);
                        p.setOptPerson(sysUser);
                    }
                }

                Dept dept = null;

                switch (m.get(13) + "")
                {
                case "1101_电子税务局_局端系统_电子报税":
                    dept = dept1;
                    break;
                case "1102_电子税务局_局端系统_在线办税":
                    dept = dept2;
                    break;
                case "1103_电子税务局_局端系统_信息服务":
                    dept = dept3;
                    break;
                default:
                    dept = dept3;
                }
                p.setDept(dept);
                p.setInsertDate(sdf.parse(m.get(18) + ""));
                p.setStatus("1");
                problemTaskBeans.add(p);
            }
            Collections.sort(problemTaskBeans);

            List<ProblemTask> problems = repository.findAllByStatus("1");

            problems.removeAll(problemTaskBeans);
            for (ProblemTask t : problems)
            {
                t.setStatus("0");
                t.setDealDate(new Date());
                problemTaskBeans.add(t);
            }
            repository.save(problemTaskBeans);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public List<ProblemTask> findProblemTaskByPeriod(Date begin, Date end)
    {
        return repository.findAllByDates(begin, end);
    }

    @Override
    public List<ProblemTask> findAll()
    {
        Iterable<ProblemTask> all = repository.findAll();

        List<ProblemTask> list = new ArrayList<ProblemTask>();
        all.forEach(single ->
            {
                list.add(single);
            });
        return list;
    }

    @Override
    public Map<String, List<ProblemTask>> dealedProblemTask(List<ProblemTask> thisWeek, Date date)
    {
        List<ProblemTask> dealed = new ArrayList<ProblemTask>();
        List<ProblemTask> add = new ArrayList<ProblemTask>();
        List<ProblemTask> left = new ArrayList<ProblemTask>();

        Map<String, List<ProblemTask>> map = new HashMap<String, List<ProblemTask>>();
        for (ProblemTask task : thisWeek)
        {
            if ("0".equals(task.getStatus()))
            {
                dealed.add(task);
            }
            else
            {
                left.add(task);
            }
            if (task.getInsertDate().after(date))
            {
                add.add(task);
            }

        }
        map.put("dealed", dealed);
        map.put("new", add);
        map.put("left", left);
        return map;
    }

    @Override
    public List<ProblemTask> findProblemTaskByStatus(String status)
    {
        return repository.findAllByStatus(status);
    }

    @Override
    public Map<String, List<ProblemTask>> group(List<ProblemTask> thisWeek)
    {

        Map<String, List<ProblemTask>> map = new HashMap<String, List<ProblemTask>>();

        List<ProblemTask> list = null;
        for (ProblemTask task : thisWeek)
        {
            String s = task.getDept().getName();
            if (map.containsKey(s))
            {
                list = map.get(s);
                list.add(task);
                map.put(s, list);
            }
            else
            {
                list = new ArrayList<ProblemTask>();
                list.add(task);
                map.put(s, list);
            }
        }
        return map;
    }

    @Override
    public ProblemTask findOne(String id)
    {
        return repository.findById(id);
    }

    @Override
    public List<ProblemTask> findNoTaskProblems()
    {
        return repository.findNoTaskProblems();
    }

    @Override
    public List<ProblemTask> batchImport(List<Map<String, Object>> list)
    {
        List<ProblemTask> problemTaskBeans = null;
        List<ProblemTask> addProblemTaskBeans = new ArrayList<ProblemTask>();
        try
        {
            problemTaskBeans = new ArrayList<ProblemTask>();
            ProblemTask p = null;
            Dept dept1 = userRepository.findDept(1);
            Dept dept2 = userRepository.findDept(2);
            Dept dept3 = userRepository.findDept(3);
            for (Map<String, Object> map : list)
            {
                p = new ProblemTask();
                p.setId(map.get("ID") + "");
                p.setLevel(map.get("JJCDMC") + "");
                p.setTitle(map.get("TITLE") + "");
                com.alibaba.druid.proxy.jdbc.ClobProxy clob = (ClobProxy) map.get("CONTENT");
                Clob c = clob.getRawClob();
                //                CLOB contentClob = (CLOB) map.get("CONTENT");
                String content = (c == null || c.length() == 0) ? null : c.getSubString((long) 1, (int) c.length());
                p.setContent(content);

                String k = map.get("OPTPERSON") + "";
                if (userMap.containsKey(k))
                {
                    p.setOptPerson(userMap.get(k));
                }
                else
                {
                    SysUser sysUser = userRepository.findUserByFullName(k);
                    if (sysUser != null)
                    {
                        userMap.put(k, sysUser);
                        p.setOptPerson(sysUser);
                    }
                }

                Dept dept = null;

                switch (map.get("DEPT") + "")
                {
                case "1101_电子税务局_局端系统_电子报税":
                    dept = dept1;
                    break;
                case "1102_电子税务局_局端系统_在线办税":
                    dept = dept2;
                    break;
                case "1103_电子税务局_局端系统_信息服务":
                    dept = dept3;
                    break;
                default:
                    dept = dept3;
                }
                p.setDept(dept);
                p.setInsertDate((Date) map.get("INSERTDATE"));
                p.setStatus("1");
                p.setStartDate((Date) map.get("YJXFKSSJ"));
                p.setEndDate((Date) map.get("YJXFSJ"));
                problemTaskBeans.add(p);
            }
            Collections.sort(problemTaskBeans);
            List<ProblemTask> problems = repository.findAllByStatus("1");
            List<ProblemTask> leftProblems = new ArrayList<ProblemTask>();
            
            leftProblems.addAll(problems);
            addProblemTaskBeans.addAll(problemTaskBeans);
            addProblemTaskBeans.removeAll(leftProblems);

            //解决的问题
            problems.removeAll(problemTaskBeans);
            for (ProblemTask t : problems)
            {
                t.setStatus("0");
                t.setDealDate(new Date());
                problemTaskBeans.add(t);
            }

            repository.save(problemTaskBeans);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return addProblemTaskBeans;
    }

}
